package com.weathereport.activity;

import java.util.ArrayList;

import com.weathereport.R;
import com.weathereport.dao.WeatherDAO;
import com.weathereport.entity.Armario;
import com.weathereport.entity.ConfortoTermico;
import com.weathereport.entity.Forecast;
import com.weathereport.entity.Local;
import com.weathereport.entity.Roupa;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class ClothesIndicationActivity extends Activity{
	
	private Context ctx;
	private WeatherDAO dao;
	private Local local;
	private int localId;
	private TextView actualTemp;
	private TextView torsoTV;
	private TextView legsTV;
	private TextView feetTV;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clothesindication);
		
		ctx = this;
		
		getReferences();
		
		// Inicializa informação que será utilizada		
		try {
			SharedPreferences settings = ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE);
	        double feel = settings.getFloat("feel", 0);

			localId = getIntent().getIntExtra("localId", 0);
			dao = new WeatherDAO(ctx); 		
	 		local = dao.getLocal(localId);
	 		
	 		Forecast localForecast = local.getForecast().get(0);
	 		
	 		actualTemp.setText(localForecast.getTempNow() + " ºC");
	 		actualTemp.setTextColor(setColor(Double.parseDouble(localForecast.getTempNow())));
	 		
	 		Double tempNow = Double.parseDouble(localForecast.getTempNow());  
	 		String sHumidity = localForecast.getHumidityNow(); 
	 		sHumidity = sHumidity.substring (0, sHumidity.length() -1);
	 		Double humidityNow = Double.parseDouble(sHumidity);
	 		Double windNow = Double.parseDouble(localForecast.getWindNow());
			double clo = ConfortoTermico.calcularClo(tempNow, tempNow, windNow, humidityNow, feel);
	
			ArrayList<Roupa> vestimenta = Armario.getInstance().escolheRoupasArmario(clo);
			for (int i = 0; i < vestimenta.size(); i++) {
				TextView campo = null;
				switch (i) {
					case Armario.ROUPA_INTIMA:
						continue;
				case Armario.CALCADO:
						campo = feetTV;
						break;
					case Armario.NORMAL_BAIXO:
					case Armario.SEGUNDA_PELE_BAIXO:
						campo = legsTV;
						break;
					case Armario.SEGUNDA_PELE_CIMA:
					case Armario.NORMAL_CIMA:
					case Armario.SEGUNDA_CAMADA_CIMA:
					case Armario.TERCEIRA_CAMADA_CIMA:
						campo = torsoTV;
						break;
					default:
						throw new Exception("Roupa inválida.");
				}
				
				campo.setText(campo.getText() + vestimenta.get(i).getNome() + "\n");
			}
		} catch (Exception e) {
			// TODO - fazer tratamento de erro aqui
		}
	}
	
	public void getReferences(){
		
		actualTemp = (TextView) findViewById(R.id.actualTempTV);
		torsoTV = (TextView) findViewById(R.id.torsoTV);		
		legsTV = (TextView) findViewById(R.id.legsTV);
		feetTV = (TextView) findViewById(R.id.feetTV);
	}
	
	public int setColor(double color){
		
		int result = 0;
		
		if (color <= 5){
			
			result = Color.BLUE;
		}
		
		if (color >= 6 && color <=14){
			
			result = Color.rgb(135, 206, 250);
		}
		
		if (color >= 15 && color <= 20){
			
			result = Color.GREEN;
		}
		
		if (color >=21 && color <= 30){
			
			result = Color.rgb(255, 165, 0);
		}
		
		if (color >= 31){
			
			result = Color.RED;
		}
		
		return result;
	}

}
